<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;

class Product extends Controller
{
    //产品列表
    public function index()
    {
        $dao = new \app\model\admin\Product();
        $c_dao = new \app\model\admin\Column();
        $c_where['status']=1;
        $c_where['type']=2;
        $c_lists = $c_dao->get_column_lists($c_where,$sort='id desc',$paginate=20);//查询栏目

        $where = [];//自 5.4 起可以使用短数组定义语法，用 [] 替代 array()
        $title = $this->request->get("title");
        if(!empty($title)){
            $where['title'] = ['like','%'.$title.'%'];
        }
        $start_time = strtotime($this->request->get("start_time"));
        $end_time = strtotime($this->request->get("end_time"));
        if($start_time){
            if(!$end_time){
                $end_time = time();
            }
            if($start_time>$end_time){
                $this->error('搜索时间不正确！', 'cards/cards_index');
            }
            $between['one'] = $start_time; //开始值
            $between['two'] = $end_time;   //结束值
            $between['val'] = 'add_time';  //条件值
            $lists = $dao->get_product_lists($where,$between,$sort='id desc',$paginate=20);
        }else{
            $lists = $dao->get_product_lists($where,$sort='id desc',$paginate=20);
        }
        $this->assign('lists', $lists);
        $this->assign('c_lists', $c_lists);
        return $this->fetch('product/index');
    }

    //产品详情
    public function detail()
    {
        $dao = new \app\model\admin\Product();
        $c_dao = new \app\model\admin\Column();
        $c_where['status']=1;
        $c_where['type']=2;
        $c_lists = $c_dao->get_column_lists($c_where,$sort='id desc',$paginate=10);
        $this->assign('c_lists', $c_lists);
        $id = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->product_detail($id);
        }
        return $this->fetch('product/detail',$data);
    }

    //产品保存修改
    public function product_save()
    {
        $dao = new \app\model\admin\Product();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['keywords'] = deletehtml($this->request->post('keywords'));
        $data['describe'] = deletehtml($this->request->post('describe'));
        $data['column'] = deletehtml($this->request->post('column'));
        $data['content'] = deletehtml($this->request->post('editorValue'));
        $data['read_num'] = deletehtml($this->request->post('read_num'));
        $data['seo_name_en'] = deletehtml($this->request->post('seo_name_en'));
        $file = $this->request->file('image');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/product_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['image'] = $updimg_status;
            }
        }
        //判断栏目是否存在
        $cdao = new \app\model\admin\Column();
        $id = $request = $this->request->get('id');
        $c_info = $cdao->column_detail($data['column']);
        if(!$c_info){
            $this->error('栏目不存在！', '/admin/article/detail?id='.$data['id']);
        }
        $data['column_name'] = $c_info['seo_name_en'];

        if(empty($data['id'])){
            $data['add_time'] = strtotime($request = $this->request->post('time'));
            $product_status = $dao->add_product($data);
        }else{
            $data['upd_time'] = strtotime($request = $this->request->post('time'));
            $where['id'] = $data['id'];
            $product_status = $dao->update_product($where,$data);
        }
        if($product_status){
            $this->redirect('/admin/product/index?');
        }else{
            $this->error('操作失败！', '/admin/product/detail?id='.$data['id']);
        }
    }

    //产品删除
    public function product_delete()
    {
        $dao = new \app\model\admin\Product();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $product_delete = $dao->delete_product([$id]);
        if($product_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$product_delete,'msg'=>'操作成功!']);
        }
    }
}


